﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Drawing ;
using System.Web.UI.WebControls;


namespace ZhiAo.CommonLibrary
{
    public static class HtmlWindow
    {

        public static bool IsAjax
        {
            get
            {
                bool result = false;
                Control scriptManager = PageHelper.CurrentPage.FindControl("ScriptManager1");
                if (scriptManager != null) result = true;
                return result;
            }
        }

        public static void AlertSuccess(string message)
        {
            message = string.Format("<span style='color:{0}\'>{1}</span>", "green", message);
            Alert(message);
        }

        private static void Alert(string message)
        {
            try
            {
                message = message.Replace("\"", ".");
                Page page = (Page)HttpContext.Current.Handler;
                string script = string.Format("alert('{0}     ');", message.Replace("'", "\\'").Replace("\"", "&#34;").Replace("\t\n", "").Replace("\n", "").Replace("'", "").Replace("'", "").Replace("\r", ""));
                page.ClientScript.RegisterStartupScript(page.GetType(), Guid.NewGuid().ToString(), script, true);

                //if (IsAjax)
                //{
                //    ScriptManager.RegisterStartupScript(page, page.GetType(), new Guid().ToString(), script, true);
                //}
                //else
                //{
                //    page.ClientScript.RegisterStartupScript(page.GetType(), new Guid().ToString(), script, true);
                //}
            }
            catch
            {
            }
        }

        public static void Alert(Exception ex)
        {
            string message = ex.Message;
            if (message.StartsWith("ORA-20001: "))
            {
                string[] array = message.Split((char)10);
                message = array[0];
                message = message.Replace("ORA-20001: ", "");
            }

            message = string.Format("<span style='color:{0}\'>{1}</span>", "red", message);
            Alert(message);

            string LogFile = "LOG" + DateTime.Now.ToString("yyyyMMdd");
            LogHelper.Write(message + ":trace=" + ex.StackTrace, LogFile);
        }

        public static void Redirect(string url)
        {
            PageHelper.CurrentPage.ClientScript.RegisterHiddenField("__HidLink", url);
        }

        public static void ImportSuccess()
        {
            PageHelper.CurrentPage.ClientScript.RegisterStartupScript(PageHelper.CurrentPage.GetType(), Guid.NewGuid().ToString(), "HasImport();", true);
        }

        public static void LinkUrl(string value)
        {
            PageHelper.CurrentPage.ClientScript.RegisterHiddenField("__HidLink", value);
        }

        public static void LinkUrlType(string value)
        {
            PageHelper.CurrentPage.ClientScript.RegisterHiddenField("__HidLinkType", value);
        }
    }
}
